app.controller("countCtrl", ["$scope",'datepickerPopupConfig', 'datepickerConfig','CountApi',"$timeout","sweetAlertUtil",
    function($scope,datepickerPopupConfig, datepickerConfig,CountApi,$timeout,sweetAlertUtil) {
      
        $scope.AREA = "AREA"
        $scope.city_name = []
        $scope.city_count = []


        $scope.vm = {
            start_date: '',
            end_date:''
        }
        $scope.start_date="";
        $scope.end_date = "";
        $scope.city = [];
        $scope.city_1 = [];
        $scope.city_2 = [];
        $scope.areaCity = []
        $scope.citystr = '';
        $scope.city_1str = '';
        $scope.city_2str = '';
        $scope.areaCityStr = '';

        // ---------------------start 初始化城市 -------------------------------
        
        function initCity(){
            CountApi.getCity().then(function(res){
                if(res.code == 200){
                    $scope.city = res.result.list
                    $timeout(function(){
                        $scope.item = "C51"
                    })
                    $timeout(function(){
                        $scope.item_1 = "C510100000000"
                    })
                    $timeout(function(){
                        $scope.item_2 = 'C510182000000'
                    })
                    $timeout(function(){
                        init_authority()
                    })
                }
            },function(){})
        }
        $scope.$watch('item',function(value){
            if($scope.item == undefined){
                return
            }
            CountApi.getCity({parentId:$scope.item}).then(function(res){
                if(res.code == 200){
                    $scope.city_1=res.result.list
                }
            },function(){})
            $scope.item_1=null;
            $scope.item_2=null;

        });
        $scope.$watch('item_1',function(value){
            if($scope.item_1 == undefined){
                return
            }
            CountApi.getCity({parentId:$scope.item_1}).then(function(res){
                if(res.code == 200){
                    $scope.city_2=res.result.list
                }
            },function(){})
            $scope.item_2=null;
        });
        // ---------------------end 初始化城市 -------------------------------



        //------------------------start 初始化选择时间组件 -------------------------------
        initDatePicker()
        function initDatePicker() {
            datepickerConfig.minMode = "day"
            datepickerConfig.datepickerMode="day"
            datepickerConfig.showWeeks = false;
            datepickerPopupConfig.showButtonBar = false

            Date.prototype.format = function(format) {
                var o = {
                    "M+": this.getMonth() + 1, //month 
                    "d+": this.getDate(), //day 
                    "h+": this.getHours(), //hour 
                    "m+": this.getMinutes(), //minute 
                    "s+": this.getSeconds(), //second 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
                    "S": this.getMilliseconds() //millisecond 
                }

                if (/(y+)/.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                }

                for (var k in o) {
                    if (new RegExp("(" + k + ")").test(format)) {
                        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                    }
                }
                return format;
            }
        }
        //------------------------end 初始化选择时间组件 -------------------------------

        //------------------------start 统计报表 -------------------------------
        
        //  初始化角色 
        
        function initRole(){
            CountApi.getArea().then(function(res){
                if(res.code == 200){
                   $scope.areaCity = res.result.citys
                   $scope.item_3 = res.result.citys[0].city_id
                   init_authority()
                }
            },function(){})            
        }

        $scope.nowYear = new Date().format("yyyy")
        init()
        function init(){
            CountApi.getUserInfo().then(function(data){
                if(data.code == 200){
                    // 判断用户
                  if(data.result.role_tag == 'AREA'){
                    $scope.AREA = "AREA"
                    initRole()
                  }else{
                    $scope.AREA = "ROOT"
                    initCity()
                  }
                    
                }
            },function(){

            }) 
        }
        function init_authority(){
            if($scope.AREA == "AREA"){
                if($scope.item_3==undefined || $scope.item_3==null){
                  sweetAlertUtil.fail("必须选择区域！");
                  return
                }
                var params = {
                    city_id:$scope.item_3,
                    startDate:$scope.start_date,
                    endDate:$scope.end_date
                }
            }else{
                if($scope.item_2==undefined || $scope.item_2==null){
                  sweetAlertUtil.fail("必须选择区域！");
                  return
                }
                var params = {
                    city_id:$scope.item_2,
                    startDate:$scope.start_date,
                    endDate:$scope.end_date
                }
            }
            CountApi.getCount(params).then(function(data){
              if(data.code == 200){
                $scope.city_name=[]
                $scope.city_count=[]
                $scope.result  = data.result
                angular.forEach($scope.result,function(v){
                    $scope.city_name.push(v.city_name)
                    $scope.city_count.push(parseInt(v.statistics))
                })

              if($scope.AREA == "AREA"){
                angular.forEach($scope.areaCity,function(v){
                  if(v.city_id == $scope.item_3){
                    $scope.areaCityStr = v.city_name
                  }
                })

                $timeout(function(){
                  $scope.areaOrdercharts.options.title.text = $scope.nowYear+'年城市:'+$scope.areaCityStr+'数据统计报表'
                  $scope.areaOrdercharts.options.xAxis.categories = $scope.city_name
                  $scope.areaOrdercharts.series[0].data=$scope.city_count
                },1000)
              }else{
                
                angular.forEach($scope.city,function(v){
                  if(v.city_id == $scope.item){
                     $scope.citystr = v.city_name
                  }
                })
                angular.forEach($scope.city_1,function(v){
                  if(v.city_id == $scope.item_1){
                     $scope.city_1str = v.city_name
                  }
                })
                angular.forEach($scope.city_2,function(v){
                  if(v.city_id == $scope.item_2){
                    $scope.city_2str = v.city_name
                  }
                })
                $timeout(function(){
                  $scope.areaOrdercharts.options.title.text = $scope.nowYear+'年城市:'+$scope.citystr+$scope.city_1str+$scope.city_2str+'数据统计报表'
                  $scope.areaOrdercharts.options.xAxis.categories = $scope.city_name
                  $scope.areaOrdercharts.series[0].data=$scope.city_count
                },1000)
              }

                    
              }
            },function(){})
        }

      	$scope.areaOrdercharts  = {
              options: {
                  title: {
                      text: ''
                  },
                  chart: {
                      type: 'column'
                  },
                  xAxis: {
                      categories: [],
                      title: {
                          text: ""
                      },
                      tickWidth:0
                  },
                  yAxis: [{
                      min: 0,
                      title: {
                          text: '举办次数',
                          align: 'high'
                      },
                      showEmpty: false,
                      labels: {
                          overflow: 'justify'
                      }
                  },{
                      title: {
                          text: '',
                          align: 'high'
                      },
                  }],
                  tooltip: {
                      enabled: true,
                      valueSuffix:'单'
                  },
                  plotOptions: {
                      series: {
                          pointWidth: 24
                      },
                      column:{
                      	colorByPoint: true
                      }
                  },
                  legend: {
                      enabled: false
                  },
                  credits: {
                      enabled: false
                  }
              },
              
              series: [{
                  name: '',
                  data: []
              }]
          }
        //------------------------end 统计报表 -------------------------------



        //------------------------start 搜索 -------------------------------
        $scope.searchByDateRange = function($event){
            $scope.start_date = $scope.vm.start_date!='' ? $scope.vm.start_date.format("yyyy-MM-dd") : ''
            $scope.end_date = $scope.vm.end_date!='' ? $scope.vm.end_date.format("yyyy-MM-dd") : '';
            init_authority()
        }
        //------------------------end 搜索 -------------------------------
    }
]);
